/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.intec.matsab.dao;

import edu.intec.matsab.persistence.PersistenceManager;
import edu.intec.matsab.persistence.Usuario;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

/**
 *
 * @author matilde
 */
public class UsuarioDAO extends BasicoDAO {

    public static Usuario isRegistered(String usuario, String passwd) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        Usuario user = null;

        try {
            Query query = manager.createNamedQuery("Usuario.findByIdUsuario");
            query.setParameter("idUsuario",usuario);
            Usuario dbUser = (Usuario) query.getSingleResult();

            if (dbUser.getPasswd().equals(passwd)) {
                user = dbUser;
            }

        } catch (NoResultException exception) {
            
        } finally {
            manager.close();
        }

        return user;
    }
    
    private static boolean checkExistingUser(String username) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean result = false;

        try {
            Query query = manager.createNamedQuery("Usuario.findByIdUsuario");
            query.setParameter("idUsuario", username);

            Usuario dbUser = (Usuario) query.getSingleResult();

            if (dbUser != null) {
                result = true;
            }
        } catch (PersistenceException exception) {
            
        } finally {
            manager.close();
        }

        return result;
    }

    
    /**
     * This method persists the given {@link Usuario} object to the database.
     * 
     * 
     * @param request {@link Usuario} object to persist
     * @return true or false if persisted successful
     */
    /*
    public static boolean createNewUser(Usuario user) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean result = false;

        if (!checkExistingUser(user.getIdUsuario())) {

            try {
                manager.getTransaction().begin();
                manager.persist(user);
                manager.getTransaction().commit();

                result = true;
                logger.info("New user: " + user.getIdUsuario() + " registrated into ILKA");
            } catch (PersistenceException exception) {
                java.util.logging.Logger.getLogger(UserAccountBean.class.getName()).log(Level.SEVERE, null, exception);
            } finally {
                manager.close();
            }
        }

        return result;
    }
    */
    
    /*
    public static boolean updateUserAccount(Usuario user) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean result = false;

        try {
            manager.getTransaction().begin();
            manager.merge(user);
            manager.getTransaction().commit();

            result = true;
            logger.info("User : " + user.getIdUsuario() + " modified ");
        } catch (PersistenceException exception) {
            java.util.logging.Logger.getLogger(UserAccountBean.class.getName()).log(Level.SEVERE, null, exception);
        } finally {
            manager.close();
        }

        return result;
    }
    */
    /*
    public static Usuario refreshUser(Usuario user) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();
        Usuario dbUser = null;

        if (user == null) {
            return dbUser;
        }

        try {
            dbUser = manager.find(Usuario.class, user.getIdUsuario());
            dbUser.getContactoCollection().isEmpty();
        } catch (PersistenceException exception) {
            java.util.logging.Logger.getLogger(UserAccountBean.class.getName()).log(Level.SEVERE, null, exception);
        } finally {
            manager.close();
        }
        return dbUser;
    }
    */
}
